package com.freewater.springboot.demo.ioc.lookupmethodinject.useannotation;

import com.freewater.springboot.demo.ioc.lookupmethodinject.AbstractCommandManager;
import com.freewater.springboot.demo.ioc.lookupmethodinject.Command;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.stereotype.Component;

/**
 * 如果要使用Spring框架的ComponentScan机制这里不能使用抽象类，如果是抽象类，会默认被忽略。
 * Note that you should typically declare such annotated lookup methods with a concrete stub implementation,
 * in order for them to be compatible with Spring’s component scanning rules where abstract classes get ignored by default.
 * This limitation does not apply to explicitly registered or explicitly imported bean classes.
 *
 * @see AbstractCommandManager
 */
@Component
public class CommandManager {

    public void process(Object commandState) {
        final Command command = createCommand();
        command.setState(commandState);
        command.execute();
    }

    /**
     * 这里不能使用抽象方法，否则类必须是抽象的
     *
     * @see AbstractCommandManager#createCommand()
     */
    @Lookup("syncCommand")
    public Command createCommand() {
        // 由于使用了@Lookup注解，因此，此处直接返回异常，由spring框架cglib生成的动态子类来处理（返回名字为syncCommand的bean）
        throw new UnsupportedOperationException();
    }
}
